﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ListenMusic.AdminSong_ServiceReference;
using ListenMusic.Models.Reposistory.Public;
using ListenMusic.Models.Reposistory.User;
using System.Transactions;
using System.IO;
using System.ServiceModel;
using ListenMusic.Singer_ServiceReference;
using ListenMusic.Musician_ServiceReference;
using ListenMusic.Models.Reposistory.Admin;
using PagedList;
namespace ListenMusic.Controllers.Admin
{
    public class ManageSongController : AdminController
    {
        private int pagesize = 20;
        //
        // GET: /ManageSong/
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult NoneActiveSong(int? pageindex)
        {
            if (pageindex == null)
            {
                pageindex = 1;
            }
            else if (pageindex < 1)
            {
                pageindex = 1;
            }       
            List<SongSinger> lst_songSinger = _manageSong.manageActiveSong(1).ToPagedList((pageindex ?? 1), pagesize).ToList();
            int count = _manageSong.manageActiveSong(1).Count();
            int totalSong = 0;
            if (count % pagesize == 0)
            {
                totalSong = (int)count / pagesize;
            }
            else
            { 
                totalSong=(int)Math.Ceiling((double)(count)/pagesize);
            }            
            ViewBag.totalSong = totalSong;
            ViewBag.pageindex = pageindex;
            ViewBag.countSong = count;
            ViewBag.lstsongSinger = lst_songSinger;
            return View("NoneActiveSong");
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult ViewAllSong(int? pageindex)
        {
            if (pageindex == null)
            {
                pageindex = 1;
            }
            else if (pageindex < 1)
            {
                pageindex = 1;
            }           
            List<SongSinger> lst = _manageSong.getAllSongSinger(1).ToPagedList((pageindex ?? 1),pagesize).ToList();
            int count = _manageSong.getAllSongSinger(1).Count();
            int totalSong = 0;
            if (count % pagesize == 0)
            {
                totalSong = (int)count / pagesize;
            }
            else
            {
                totalSong = (int)Math.Ceiling((double)(count) / pagesize);
            }       
            ViewBag.pageindex = pageindex;
            ViewBag.totalSong = totalSong;
            ViewBag.countSong = count;
            ViewBag.allSong = lst;
            return View();
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult uploadSong()
        {
            CountryReposistory _country = new CountryReposistory();
            CategoryReposistory _category = new CategoryReposistory();
            ViewBag.country = _country.getAllCountry();
            ViewBag.category = _category.getAllCategory();
            return View("UploadSongPage");
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult ConfirmUploadSong(IEnumerable<HttpPostedFileBase> filename, string songurl, string songname, string lyrics, string singername, string musicianname, int country, int category)
        {
            AdminUploadReposistory _adminUpload = new AdminUploadReposistory();
            string pathSong = Server.MapPath("~/Music/");
            string pathVideo = Server.MapPath("~/Music/Videos/");
            bool rs = _adminUpload.ConfirmUploadSong(filename, songname,lyrics,singername,musicianname,country,category,pathSong,pathVideo);
            if (rs)
            {
                ViewBag.messageSongName = songname;
                return RedirectToAction("MessageUploadSuccess");
            }
            else
            {
                return RedirectToAction("uploadSong");
            }
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult updateSong(HttpPostedFileBase filename, int sid, string songurl,string singername, string songname, string musicianname,  int country, int category, string lyrics)
        {           
                string pathMP3 = Server.MapPath("~/Music/");
                string pathVideo = Server.MapPath("~/Music/Videos/");
                AdminUpdateReposistory _adminUpdate = new AdminUpdateReposistory();
                bool rs = _adminUpdate.UpdateSong(filename, sid, songurl,songname, lyrics, singername, musicianname, country, category, pathMP3, pathVideo);
                if (rs)
                {
                    return RedirectToAction("NoneActiveSong", new { pageindex = 1 });
                }
                else
                {
                    return RedirectToAction("UpdateSongPage", new { songid = sid });
                }           
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult UpdateSongPage(int songid)
        {
            CountryReposistory _country = new CountryReposistory();
            CategoryReposistory _category = new CategoryReposistory();
            ViewBag.country = _country.getAllCountry();
            ViewBag.category = _category.getAllCategory();
            SongSinger songSinger = _manageSong.getSongSinger_bySongID(songid);
            return View(songSinger);
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public JsonResult activeSong(int songid)
        {
            bool rs = _manageSong.activeSong(songid);
            return Json(rs,JsonRequestBehavior.AllowGet);
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public JsonResult deleteSong(int songid)
        {
            bool rs = _manageSong.deleteSong_bySongID(songid);
            return Json(rs, JsonRequestBehavior.AllowGet);
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult MessageUploadSuccess()
        {
            return View();
        }
        
    }
}
